package com.mamesoft.medicipazienti.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;

public class IndexServlet extends HttpServlet {

    private static Logger log = Logger.getLogger(ConfermaRichiamiServlet.class);

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("role");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("    <head>");
            out.println("        <title>Vaccino Manger</title>");
            out.println("        <link rel=\"stylesheet\" type=\"text/css\" href=\"/MediciPazienti/variant-stream.css\" title=\"Variant Stream\" media=\"screen,projection\" />");

            if (role != null) {
                response.sendRedirect("/MediciPazienti/" + role.toLowerCase() + "/welcome");
            }

            out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">       ");
            out.println("    </head>");
            out.println("    <body>");
            out.println("        <div id =\"wrap\">");
            out.println("            <div id=\"header\">");
            out.println("                <div id =\"topmenu\">");
            out.println("                    <p>");
            out.println("                        <a href=\"registrati.html\">Registrati</a>");
            out.println("                    </p>");
            out.println("                </div>");
            out.println("                <div id=\"title\">");
            out.println("                    <h1>Vaccino Manager</h1><br/>");
            out.println("                    <h2 class =\"slogan\">I tuoi vaccini contano</h2>");
            out.println("                </div>");
            out.println("            </div>");
            out.println("            <div id=\"sidebar\">");
            out.println("                <form action=\"login_servlet\" method=\"POST\">");
            out.println("                    <p class =\"sidemenu\">");
            out.println("                        Username: <br/>");
            out.println("                        <input type=\"text\" name=\"user\" value=\"\" />");
            out.println("                    </p>");
            out.println("                    <p class =\"sidemenu\">");
            out.println("                        Password: <br/>");
            out.println("                        <input type=\"password\" name=\"password\" value=\"\" />");
            out.println("                    </p>");
            out.println("                    <p class =\"sidemenu\">");
            out.println("                        <select name=\"role\">");
            out.println("                            <option>-->Scegli un ruolo</option>");
            out.println("                            <option>Medico</option>");
            out.println("                            <option>Paziente</option>");
            out.println("                        </select>");
            out.println("                    </p>");
            out.println("                    <p>");
            out.println("                        <input type=\"reset\" value=\"Cancella\" />");
            out.println("                        <input type=\"submit\" value=\"Login\" />");
            out.println("                    </p>");
            out.println("                </form>            ");
            out.println("            </div>");
            out.println("            <div id =\"content\">");
            out.println("                <div class=\"post\">");
            out.println("                    <h2>Benvenuto!</h2>");
            out.println("                    <table border=\"0\">                       ");
            out.println("                        <tbody>");
            out.println("                            <tr>");
            out.println("                                <td>");
            out.println("                                    <img src=\"images/index_foto.jpg\" width=\"230\" height=\"307\" alt=\"Foto Siringa\"/>");
            out.println("                                </td>");
            out.println("                                <td valign =\"top\"> ");
            out.println("                                    Sarai vaccinato! Che ti piaccia o no, quindi preparati a soffrire =)");
            out.println("                                </td>");
            out.println("                            </tr>");
            out.println("                        </tbody>");
            out.println("                    </table>");
            out.println("                </div>");
            out.println("            </div>");
            out.println("            <div id=\"footer\">");
            out.println("                <p class=\"centered\">Copyright &copy; 2011 <a href=\"/MediciPazienti/index.html\">Mamesoft</a>");
            out.println("                    Progetto Programmazione per il web A.A.2011/2012</p>");
            out.println("            </div>");
            out.println("        </div>");
            out.println("    </body>");
            out.println("</html>");

        } catch (Exception ex) {
            session.invalidate();
            log.error("Generic error: " + ex.toString());
            response.sendRedirect("/MediciPazienti/general_error.html");
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
